<?php
namespace app\admin\controller;
use app\admin\model\Article as ArticleModel;
use app\admin\model\Cate as CateModel;
use app\common\service\CateService;
use think\facade\Request;
use app\admin\traits\Result;
use think\validate;
use think\Db;
use library\ArrayOperate;

class Cate extends Common
{
    
    public function index(){
		//$cate_list = CateService::cate_tree_three();
		
		$list = CateModel::field('id,parent_id,title')->where('status',1)->order('sort asc,id asc')->select();
        //$data = list_to_tree($list->toArray(), 'id', 'parent_id', 'children');
		$data = ArrayOperate::arrayToTreexc($list->toArray());
		$cate_list = ArrayOperate::treeToArray($data);
        
        return $this->fetch('',['cate_list'=>$cate_list]);
        
    }

     
    
    public function edit(){
       if ($this->request->isPost()) {
            $data = $this->request->post();
			
			$validate = validate('Cate');
			if (!$validate->check($data)) {
				//令牌数据无效时重置令牌
				$validate->getError() != '令牌数据无效' ? $token = Request::token() : $token = '';
				$msg = Result::error($validate->getError(), null, ['token' => $token]);
				return $msg;
			}
				
            if ($data['id']) {
                if($data['id'] == $data['parent_id']){
                    $this->error('上级分类不能选自己');
                }
                //编辑
                $res = CateModel::update($data, ['id' => $data['id']]);
                if ($res) {
                    $msg = Result::success('编辑成功', url('/admin/cate-list'));
                } else {
                    $msg = Result::error('编辑失败');
                }
            } else {
                //添加
                $CateModel = new CateModel;
                $res = $CateModel->allowField(true)->save($data);
                if ($res) {
                    $msg = Result::success('添加成功', url('/admin/cate-list'));
                } else {
                    $msg = Result::error('添加失败');
                }
            }
            return $msg;
        } else { 
            $id = $this->request->get('id', 0, 'intval');
            if ($id) {
                $list = CateModel::where('id', '=', $id)->find();
                $this->assign('list', $list);
            }
			$parent_id = $list['parent_id']??0;
            //$cate_list = CateService::cate_tree_two();
            //$this->assign('cate_list', $cate_list);
			
			$list = CateModel::field('id,parent_id,title')->where('status',1)->order('sort asc,id asc')->select();
        	$cate_option_str = ArrayOperate::arrayToSelect($list->toArray(), $parent_id);
			//dump($cate_option_str);
			$this->assign('cate_option_str', $cate_option_str);
            return $this->fetch();
        } 
        
    }
    
    
    public function del(){
        $id = $this->request->param('id', 0, 'intval');
        if ($id) {
            $flag_cate = CateModel::where('parent_id',$id)->value('id');
            if(!empty($flag_cate)){
                $this->error('请先删除子分类');
            }
            $flag_art = ArticleModel::where('cate_id',$id)->value('a_id');
            if(!empty($flag_art)){
                $this->error('请先删除此分类下的文章');
            }
            $CateModel = CateModel::where('id='.$id)->find();
            $res = $CateModel->delete();
            if ($res) {
                $msg = Result::success('删除成功', url('/admin/cate-list'));
            } else {
                $msg = Result::error('删除失败');
            }

            return $msg;
            
        } else {
            $this->error('参数错误');
        }
        
    }
    
    
    //清空
	public function delall()
    {
        $res = Db::name('cate')->delete(true);
        if ($res) {
            $msg = Result::success('操作成功');
        } else {
            $msg = Result::error('操作失败');
        }
        return $msg;
    }
	
	public function getAllChildId()
    {
		$id = $this->request->param('id', 0, 'intval');
		if ($id) {
			$ids = getAllChildId($id); 
			return $ids;
			 
		}else {
            $this->error('参数错误');
        }
        
    }
    
}



?>